package cn.itcast.goods.controller;

import cn.itcast.annotation.Permission;
import cn.itcast.constant.PermissionConstant;
import cn.itcast.goods.dto.CategoryDTO;
import cn.itcast.goods.dto.CategoryPageQueryDTO;
import cn.itcast.goods.pojo.Category;
import cn.itcast.goods.service.CategoryService;
import cn.itcast.result.PageBean;
import cn.itcast.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping({"/admin/category","/user/category"})
@Api(tags = "分类相关接口")
@Slf4j
@Permission(value = PermissionConstant.CATEGORIZE_ADMINISTRATIVE_PERMISSIONS)
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


//    /**
//     * 分类查询
//     *
//     * @return
//     */
//    @ApiOperation("分类查询")
//    @GetMapping("/list")
//    public Result<List<Category>> getByType(){
//        log.info("分类条件查询");
//        List<Category> categories = categoryService.getCategory();
//
//        return Result.success(categories);
//    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @ApiOperation("根据类型查询分类")
    @GetMapping("/list")
    public Result<List<Category>> getByType(Integer type){
        List<Category> categorys = categoryService.getByType(type);

        return Result.success(categorys);
    }

    //============

    /**
     * 分类分页查询
     *
     * @param dto
     * @return
     */
    @ApiOperation("分类分页查询")
    @GetMapping("/page")
    public Result<PageBean> pageQuery(CategoryPageQueryDTO dto){
        log.info("分类管理分页查询：{}",dto);
        PageBean pageBean = categoryService.pageQuery(dto);

        return Result.success(pageBean);
    }

    /**
     * 修改分类
     *
     * @param dto
     * @return
     */
    @ApiOperation("修改分类")
    @PutMapping
    public Result<String> update(@RequestBody CategoryDTO dto){
        log.info("修改分类：{}",dto);
        categoryService.update(dto);

        return Result.success();
    }

    /**
     * 启动、禁用分类
     *
     * @param status
     * @return
     */
    @ApiOperation("启动、禁用分类")
    @PostMapping("status/{status}")
    public Result<String> startOrStop(@PathVariable Integer status, Long id){
        log.info("启动、禁用分类status:{}id{}",status,id);
        categoryService.startOrStop(id,status);

        return Result.success();
    }

    /**
     * 根据id删除分类
     *
     * @param id
     */
    @ApiOperation("根据id删除分类")
    @DeleteMapping
    public Result<String> deleteById(Integer id) throws Exception {
        categoryService.deleteById(id);

        return Result.success();
    }



}
